package com.java.file;

import java.io.File;
import java.io.FilenameFilter;
import java.util.regex.Pattern;
/**
 * 目录过滤器筛选我们需要的文件
 * @author admin
 *
 */
public class DirList {
	public static void main(String[] args) {
		File path = new File(".");
		//正则表达式
		String[] list = path.list(new DirFilter("^.*?\\.(xml|jpeg|bmp|gif)$"));
		for (int i = 0; i < list.length; i++)
			System.out.println(list[i]);
	}
}

class DirFilter implements FilenameFilter {
	private Pattern pattern;

	public DirFilter(String regex) {
		pattern = Pattern.compile(regex);
	}

	public boolean accept(File dir, String name) {
		// Strip path information, search for regex:
		return pattern.matcher(new File(name).getName()).matches();
	}
}